﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Eco.Handles;
using Descriptions_DomainModel.Model;
using Eco.Services;
using Eco.ObjectImplementation;

namespace DomainModelEditor
{
    public partial class NodesEditForm : Form
    {
        private IUndoService undo;
        private bool result = false;
        private EcoSpace ecoSpace;

        public NodesEditForm(EcoSpace ecoSpace, ElementHandle master, string expression)
        {
            if (ecoSpace == null)
                throw new NullReferenceException("EcoSpace cannot be null!");
            if (master == null)
                throw new NullReferenceException("Master cannot be null!");

            InitializeComponent();

            undo = ecoSpace.GetEcoService<IUndoService>();

            if (undo == null)
                throw new NullReferenceException("UndoService must be registered in EcoSpace for NodeEditForm to work!");

            this.ecoSpace = ecoSpace;

            NodesEH.RootHandle = master;
            NodesEH.Expression = expression;
            try
            {
                //проверяем, корректный ли нам дали текст OCL-выражения
                var q = NodesEH.Element.GetAsIList<Node>();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        #region Nodes

        private void NodesDGV_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (NodesCMH.Element == null)
                return;

            var editform = new NodeEditForm(ecoSpace, NodesCMH.Element.AsObject as Node);
            editform.ShowDialog();
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            NodesEH.Element.GetAsIList<Node>().Add(
                new Node(ecoSpace) { BelongingID = "DomainModel"});
        }

        private void DelButton_Click(object sender, EventArgs e)
        {
            if (NodesCMH.Element == null)
                return;

            try
            {
                (NodesCMH.Element.AsObject as Node).Methods.Clear();
                (NodesCMH.Element.AsObject as Node).NamedStates.Clear();
                (NodesCMH.Element.AsObject as Node).StateSet.AsIObject().Delete();
                (NodesCMH.Element.AsObject as Node).AsIObject().Delete();
            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show("Сначала необходимо удалить все вложенные узлы!",
                    "Удаление", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
        } 

        #endregion

        private void NodesEditForm_Load(object sender, EventArgs e)
        {
            undo.StartTransaction();
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            result = true;
            Close();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            result = false;
            Close();
        }

        private void NodesEditForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (result)
                undo.CommitTransaction();
            else
                undo.RollbackTransaction();
        }
    }
}